package short_semester.questionnaire_planet.service.analyse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.dao.ScoreOptionDao;
import short_semester.questionnaire_planet.dao.ScoreQuestionDao;
import short_semester.questionnaire_planet.dao.ScoreQuestionRecordDao;
import short_semester.questionnaire_planet.pojo.*;

import short_semester.questionnaire_planet.response.fillInResponse.FillInResponse;
import short_semester.questionnaire_planet.response.fillResponse.OptionRecordResponse;
import short_semester.questionnaire_planet.response.fillResponse.QuestionRecordResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ScoreQuestionRecordService {

    @Autowired
    ScoreQuestionRecordDao scoreQuestionRecordDao;

    @Autowired
    QuestionnaireDao questionnaireDao;
    
    @Autowired
    ScoreOptionDao scoreOptionDao;

    public List<FillInResponse> getResponse(
            QuestionnaireRecord questionnaireRecord, Integer questionnaireId) {
        List<FillInResponse> result = new ArrayList<>();
        Questionnaire questionnaire = questionnaireDao.findById(questionnaireId).get();
        List<ScoreQuestion> list = scoreQuestionDao.findByQuestionnaire(questionnaire);
        for (ScoreQuestion eachQuestion: list) {
            Optional<ScoreQuestionRecord> optional = scoreQuestionRecordDao.
                    findByQuestionnaireRecordAndScoreQuestion(questionnaireRecord, eachQuestion);
            if (!optional.isPresent()) {
                FillInResponse response = new FillInResponse();
                response.setType("score");
                response.setIsAnswer(false);
                response.setId(eachQuestion.getId());
                result.add(response);
            }
            else {
                FillInResponse response = new FillInResponse();
                ScoreQuestionRecord record = optional.get();
                response.setType("score");
                response.setIsAnswer(true);
                response.setOptionId(record.getAnswer());
                response.setId(eachQuestion.getId());
                result.add(response);
            }
        }
        return result;
    }

    @Autowired
    ScoreQuestionDao scoreQuestionDao;
    
    public void addRecord(
            Integer questionId,
            QuestionnaireRecord questionnaireRecord,
            Integer optionId
    ) {
        Optional<ScoreQuestion> scoreQuestion = scoreQuestionDao.findById(questionId);
        if (!scoreQuestion.isPresent())
            return;
        ScoreQuestion question = scoreQuestion.get();
        Optional<ScoreQuestionRecord> ret = scoreQuestionRecordDao
                .findByQuestionnaireRecordAndScoreQuestion(questionnaireRecord, question);
        ScoreQuestionRecord record;
        if (ret.isPresent()) {
            record = ret.get();
//            ScoreOption option = scoreOptionDao.getById(optionId);
//            option.setCount(option.getCount()+1);
//            scoreOptionDao.save(option);
//            
//            option = scoreOptionDao.getById(record.getAnswer());
//            option.setCount(option.getCount()-1);
//            scoreOptionDao.save(option);
        }
        else {
            record = new ScoreQuestionRecord();
//            question.setCount(question.getCount() + 1);
            record.setScoreQuestion(question);
            
            scoreQuestionDao.save(question);
            
            ScoreOption option = scoreOptionDao.getById(optionId);
//            option.setCount(option.getCount()+1);
            scoreOptionDao.save(option);
        }
        record.setQuestionnaireRecord(questionnaireRecord);
        record.setAnswer(optionId);
        scoreQuestionRecordDao.save(record);
    }
    
    public void submitRecord(QuestionnaireRecord questionnaireRecord){
        for (ScoreQuestionRecord questionRecord : scoreQuestionRecordDao.findAllByQuestionnaireRecord(questionnaireRecord)) {
            if(questionRecord.getAnswer()!=null){
                scoreQuestionDao.submitQuestion(questionRecord.getScoreQuestion().getId());
                scoreOptionDao.submitOption(questionRecord.getAnswer());
            }
        }
    }

    //获取题目记录信息
    public QuestionRecordResponse getRecord(QuestionnaireRecord record, Integer questionId){
        //题目基本信息
        ScoreQuestion question = scoreQuestionDao.getById(questionId);
        ScoreQuestionRecord questionRecord = scoreQuestionRecordDao.findByQuestionnaireRecordAndScoreQuestion(record, question).get();
        QuestionRecordResponse questionRecordResponse = new QuestionRecordResponse(question,questionRecord);

        //选项信息
        for (ScoreOption option : scoreOptionDao.findByScoreQuestion(question)) {
            OptionRecordResponse optionRecordResponse = new OptionRecordResponse(option);
            questionRecordResponse.getOptions().add(optionRecordResponse);
        }
        return  questionRecordResponse;
    }
}
